package com.sky.face.service;

import com.alibaba.fastjson.JSONObject;
import com.sky.face.config.FaceConfig;
import com.sky.face.util.Base64Util;
import com.sky.face.util.FileUtil;
import com.sky.face.util.HttpUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 人脸库管理
 */
public class FaceDatabaseService {

    public static String add() {
        // 请求url
        String url = FaceConfig.ADD_FACE_URL;
        try {
            //byte[] img = FileUtil.readFileByBytes("/Volumes/Andy/两寸蓝底照.jpeg");
            byte[] img = FileUtil.readFileByBytes("/Volumes/Andy/WechatIMG7.jpeg");
            Map<String, Object> map = new HashMap<>();
            map.put("image", Base64Util.encode(img));
            map.put("group_id", "SkyZhang");
            map.put("user_id", "18025431580");
            map.put("user_info", "Sky test face recognition");
            map.put("liveness_control", "NORMAL");
            map.put("image_type", "BASE64");
            map.put("quality_control", "LOW");
            map.put("action_type", "REPLACE");

            String param = JSONObject.toJSONString(map);

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间。
            String accessToken = AuthService.getAuth();

            String result = HttpUtil.post(url, accessToken, "application/json", param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String update(){
        // 请求url
        String url = FaceConfig.UPDATE_FACE_URL;
        try {
            byte[] image = FileUtil.readFileByBytes("/Volumes/Andy/WechatIMG9.jpeg");
            Map<String, Object> map = new HashMap<>();
            map.put("image", Base64Util.encode(image));
            map.put("group_id", "sky");
            map.put("user_id", "18025431580");
            map.put("user_info", "Sky test face recognition");
            map.put("liveness_control", "NORMAL");
            map.put("image_type", "BASE64");
            map.put("quality_control", "LOW");

            String param = JSONObject.toJSONString(map);

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间.
            String accessToken = AuthService.getAuth();

            String result = HttpUtil.post(url, accessToken, "application/json", param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String addUserGroup(String groupId){
        String groupList = getListUserGroup();
        if (groupList.indexOf(groupId) > -1) {
            return "已经存在该用户组";
        }
        try{
            Map<String, Object> map = new HashMap<>();
            map.put("group_id", groupId);

            String param = JSONObject.toJSONString(map);

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间.
            String accessToken = AuthService.getAuth();

            String result = HttpUtil.post(FaceConfig.ADD_USER_GROUP, accessToken, "application/json", param);
            System.out.println(result);
        return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getListUserGroup(){

        try {
            Map<String, Object> map = new HashMap<>();
            map.put("start", 0);
            map.put("length", 100);

            String param = JSONObject.toJSONString(map);

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = AuthService.getAuth();

            String result = HttpUtil.post(FaceConfig.GET_USER_GROUP, accessToken, "application/json", param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String faceSearch(String userId, String groupId){
        try {
            byte[] img = FileUtil.readFileByBytes("/Volumes/Andy/u=1533263413,4245234134&fm=26&gp=0.jpg");
            Map<String, Object> map = new HashMap<>();
            map.put("image", Base64Util.encode(img));
            map.put("liveness_control", "NORMAL");
            map.put("group_id_list", groupId);
            map.put("image_type", "BASE64");
            map.put("quality_control", "LOW");
            map.put("user_id", userId);

            String param = JSONObject.toJSONString(map);

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = AuthService.getAuth();

            String result = HttpUtil.post(FaceConfig.FACE_SEARCH_URL, accessToken, "application/json", param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String idMatch(String id, String name){

        try {
            Map<String, Object> map = new HashMap<>();
            map.put("id_card_number", id);
            map.put("name", name);
            String param = JSONObject.toJSONString(map);
            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = AuthService.getAuth();

            String result = HttpUtil.post(FaceConfig.ID_MATCH_URL, accessToken, "application/json", param);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
